﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Class_KetNoi;

namespace GiaoDien
{
    public partial class uc_ChuyenCongTac : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_ChuyenCongTac()
        {
            InitializeComponent();
        }
        KetNoi_ChuyenCongTac kn = new KetNoi_ChuyenCongTac();
        public void LoadDuLieu()
        {
            gridControl1.DataSource = kn.load_ChuyenCongTac();
            string MaCV = "select * from tbl_ChucVu";
            cobMaChucVuMoi.DataSource = kn.taobang(MaCV);
            cobMaChucVuMoi.DisplayMember = "MaCV";
            string MaPB = "select * from tbl_PhongBan";
            cobMaPBMoi.DataSource = kn.taobang(MaPB);
            cobMaPBMoi.DisplayMember = "MaPB";

        }
        public void Nut()
        {
            btnthem.Enabled = false;
            btnsua.Enabled = false;
            btnxoa.Enabled = false;

            btnluu.Enabled = true;
            btnhuy.Enabled = true;
        }
        private void uc_ChuyenCongTac_Load(object sender, EventArgs e)
        {
            kn.connect();
            LoadDuLieu();
            lbMaChuyen.Hide();
            txtMaChuyen.Hide();
            btnhuy.Enabled = false;
            btnluu.Enabled = false;
        }
        int flag;
        private void btnthem_Click(object sender, EventArgs e)
        {
            flag = 0;
            Nut();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            flag = 1;
            Nut();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            kn.xoa_ChuyenConngTac(txtMaChuyen.Text );
            LoadDuLieu();
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                kn.them_ChuyenConngTac(txtMaNV.Text ,date_NgayChuyen.Text,cobMaChucVuCu.Text ,cobMaChucVuMoi.Text ,CobMaPBCu.Text ,cobMaPBMoi.Text);
            }
            if (flag == 1)
            {
                kn.sua_ChuyenConngTac(txtMaChuyen.Text ,txtMaNV.Text, date_NgayChuyen.Text, cobMaChucVuCu.Text, cobMaChucVuMoi.Text, CobMaPBCu.Text, cobMaPBMoi.Text);
            }
            LoadDuLieu();
            btnthem.Enabled = true;
            btnsua.Enabled = true;
            btnxoa.Enabled = true;

            btnluu.Enabled = false;
            btnhuy.Enabled = false;
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            txtMaChuyen.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Chuyển").ToString();
            txtMaNV.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Nhân Viên").ToString();
            date_NgayChuyen.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Ngày Chuyển").ToString();
            cobMaChucVuCu.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Chức Vụ Cũ").ToString();
            cobMaChucVuMoi.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Chức Vụ Mới").ToString();
            CobMaPBCu.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Phòng Ban Cũ").ToString();
            cobMaPBMoi.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Phòng Ban Mới").ToString();
        }

        private void txtMaNV_TextChanged(object sender, EventArgs e)
        {
            string s = "select * from tbl_NhanVien where MaNV='"+txtMaNV.Text+"'";
            DataTable dt = new DataTable();
            try
            {
                dt = kn.taobang(s);
            }
            catch (Exception)
            { }
            if (dt.Rows.Count != 0)
            {
                errorProvider1.Clear();
                cobMaChucVuCu.DataSource = kn.taobang(s);
                cobMaChucVuCu.DisplayMember = "MaCV";
                CobMaPBCu.DataSource = kn.taobang(s);
                CobMaPBCu.DisplayMember = "MaPB";
            }
            else
            {
                errorProvider1.SetError(txtMaNV, "Mã nhân viên không tồn tại!");
                cobMaChucVuCu.ResetText();
                CobMaPBCu.ResetText();
                txtMaNV.Focus();
            }
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            txtMaNV.ResetText();
            CobMaPBCu.ResetText();
            cobMaChucVuCu.ResetText();
        }
    }
}
